#include <QDebug>
#include "vchartwidget.h"

using namespace std;

bool bigger_than(const RelatWellData& s1, RelatWellData& s2)
{
    return (s1.eDepth > s2.eDepth);
}

bool little_than(const RelatWellData& s1, RelatWellData& s2)
{
    return (s1.eDepth <= s2.eDepth);
}

vChartWidget::vChartWidget(QWidget *parent) : QWidget(parent)
{
    //设置整体布局
    QHBoxLayout *layoutall = new QHBoxLayout(this);
    //设置波形窗口
    vCShowqchart = new QChart();
    vChartView = new QChartView(vCShowqchart);
//    vChartView->setChart(vCShowqchart);

    //设置坐标
    vPressAxisX = new QValueAxis();
    //    vPressAxisX->setTitleText("X轴");
    vPressAxisX->setRange(-12,-5);
    vPressAxisX->setTickCount(19);
    vCShowqchart->addAxis(vPressAxisX,Qt::AlignBottom);

    //设置坐标
    vPressAxisY = new QValueAxis();
    //    vPressAxisY->setTitleText("Y轴");
    vPressAxisY->setTickCount(19);
    vPressAxisY->setRange(10,22);
    vCShowqchart->addAxis(vPressAxisY,Qt::AlignLeft);

    /* 定义原始井线条 */
    originSeries = new QLineSeries();
    originSeries->setColor(Qt::black);
    originSeries1 = new QLineSeries();



    /* 定义突出散点 */
    scatterSeries = new QScatterSeries();
    scatterSeries->setMarkerSize(10); // 设置圆点大小
    scatterSeries->setColor(Qt::blue);  // 设置圆点颜色

    scatterSeries1 = new QScatterSeries();
    scatterSeries1->setMarkerSize(10); // 设置圆点大小
    // 将散点序列中的点添加到散点序列中
    for (const QPointF &point : originSeries->points()) {
        scatterSeries->append(point);
        QPointF abc = calculateNewPoint(point,210,1);
        scatterSeries1->append(abc);
    }

    vCShowqchart->addSeries(originSeries);
    vCShowqchart->addSeries(originSeries1);
    vCShowqchart->addSeries(scatterSeries);
    vCShowqchart->addSeries(scatterSeries1);
//    vCShowqchart->createDefaultAxes();
    // 将坐标轴添加到图表
    vCShowqchart->setAxisX(vPressAxisX, originSeries);
    vCShowqchart->setAxisY(vPressAxisY, originSeries);
    vCShowqchart->setAxisX(vPressAxisX, originSeries1);
    vCShowqchart->setAxisY(vPressAxisY, originSeries1);
    vCShowqchart->setAxisX(vPressAxisX, scatterSeries);
    vCShowqchart->setAxisY(vPressAxisY, scatterSeries);
    vCShowqchart->setAxisX(vPressAxisX, scatterSeries1);
    vCShowqchart->setAxisY(vPressAxisY, scatterSeries1);

    vChartView->setRenderHint(QPainter::Antialiasing);
    vCShowqchart->legend()->setVisible(false);

    /* 定义当前井线条 */
//    // 定义梯形的四个顶点
//    QPolygonF trapezoid;
//    trapezoid << QPointF(vCShowqchart->plotArea().left() + 50, vCShowqchart->plotArea().top() + 50);   // 左上
//    trapezoid << QPointF(vCShowqchart->plotArea().left() + 150, vCShowqchart->plotArea().top() + 50);  // 右上
//    trapezoid << QPointF(vCShowqchart->plotArea().left() + 200, vCShowqchart->plotArea().bottom() - 50); // 右下
//    trapezoid << QPointF(vCShowqchart->plotArea().left() + 0, vCShowqchart->plotArea().bottom() - 50);  // 左下

//    // 创建 QGraphicsPolygonItem 梯形对象
//    QGraphicsPolygonItem *trapezoidItem = new QGraphicsPolygonItem(trapezoid);

//    // 设置梯形的颜色和边框
//    trapezoidItem->setPen(QPen(Qt::green, 0.82));  // 绿色边框，宽度为2
////    trapezoidItem->setBrush(QBrush(QColor(0, 255, 0, 100)));  // 半透明绿色填充

//    // 将梯形添加到 QChart 的场景中
//    vCShowqchart->scene()->addItem(trapezoidItem);

    layoutall->addWidget(vChartView);


}

vChartWidget::~vChartWidget(void)
{

}


/* 根据角度和距离计算新点的位置 (输入中心点并且输入角度和相对距离 确定相对点坐标) */
QPointF vChartWidget::calculateNewPoint(const QPointF &center, double angle, double distance)
{
    // 将角度转换为弧度
    double radian = qDegreesToRadians(angle);

    // 三角函数计算新点的 x 和 y 坐标
    double newX = center.x() + distance * qCos(radian);
    double newY = center.y() + distance * qSin(radian);

    return QPointF(newX, newY);
}


/* 绘制以点为中心的体型垂直于两点之间的连线 */
// 旋转点 (x, y) 绕中心点(宽，高) (x0, y0) 旋转 angle 角度（度）
QPointF vChartWidget::rotatePoint(const QPointF &point, const QPointF &center, double angle) {
    double radian = qDegreesToRadians(angle);  // 将角度转换为弧度

    double x_new = center.x() + (point.x() * qCos(radian) - point.y() * qSin(radian));
    double y_new = center.y() + (point.x() * qSin(radian) + point.y() * qCos(radian));

    return QPointF(x_new, y_new);
}



/* 计算并绘制新井线形 */
void vChartWidget::vCulDarwNewLine(QList<RelatWellData> pdata)
{
    //定义累积轴数据
    float diffaccuX = -5;
    float diffaccuY = 11;
    QPointF newline;
    QPointF oldline;

    /* 计算数据比例 */
    quint32 count = pdata.size();

    /* 排序数据并且计算数据总数 */
    // 使用 std::sort 进行排序
    std::sort(pdata.begin(), pdata.end(),little_than);
    //获取总深度
//    for(int m=0; m<count; m++)
//    {
//      qDebug() << pdata[m].eDepth <<  pdata[m].eDistan << pdata[m].eAzimuth;
//    }
    AllDepth = pdata[count-1].eDepth - pdata.begin()->eDepth;
    originSeries->clear();
    scatterSeries->clear();
    originSeries1->clear();
    scatterSeries1->clear();
//            // 获取图表的场景
//            QGraphicsScene *scene = vCShowqchart->scene();
//            if(scene != nullptr)
//                scene->clear();
//            // 遍历场景中的所有项
//            for (QGraphicsItem *item : scene->items()) {
//                // 检查项是否为 QGraphicsTextItem
//                if (QGraphicsTextItem *textItem = qgraphicsitem_cast<QGraphicsTextItem*>(item)) {
//                    // 从场景中移除文本项
//                    scene->removeItem(textItem);
//                    delete textItem; // 释放内存
//                }
//            }

    //逐个数据进行
    for (int i = 0; i < count; ++i)
    {
        if(i >= 1)
        {
            float diff = pdata[i].eDepth - pdata[i - 1].eDepth;  // 计算当前值与上一个值的差值
            diffaccuX = diffaccuX-(6.5*(diff/AllDepth));
            diffaccuY = diffaccuY+(11*(diff/AllDepth));
            oldline = QPointF(diffaccuX, diffaccuY);
            //逐个计算赋值测试井深的点位
            originSeries->append(oldline);
            scatterSeries->append(oldline);
            //绘制新线
            newline = calculateNewPoint(oldline, pdata[i].eAzimuth , pdata[i].eDistan);
//            qDebug() << pdata[i].eDistan << pdata[i].eAzimuth;
            originSeries1->append(newline);
            scatterSeries1->append(newline);
            QString pointtext = QString::number(pdata[i].eDepth) + ";" + QString::number(pdata[i].eAzimuth) + ";" + QString::number(pdata[i].eDistan) ;
            addCustomLabel(vCShowqchart, newline.rx()+0.01, newline.ry()+0.3, pointtext);

//            // 创建文本说明
//            QGraphicsTextItem *textItem = new QGraphicsTextItem(labels[i]);
//            textItem->setPos(diffaccuX, diffaccuY); // 设置文本位置
//            textItem->setDefaultTextColor(Qt::black);
//            vCShowqchart->addToScene(textItem); // 将文本添加到图表场景
//            //绘制连线与梯形

        }
        else
        {
            oldline = QPointF(-5, 11);
            originSeries->append(oldline);
            newline = calculateNewPoint(oldline, pdata[i].eAzimuth , pdata[i].eDistan);
            originSeries1->append(newline);
            scatterSeries1->append(newline);

        }
    }

}









